package com.lam.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lam.dormitory.entity.Admin;
import com.lam.dormitory.entity.Student;
import com.lam.dormitory.entity.User;
import com.lam.dormitory.entity.UserRole;
import com.lam.dormitory.mapper.AdminMapper;
import com.lam.dormitory.mapper.StudentMapper;
import com.lam.dormitory.mapper.UserMapper;
import com.lam.dormitory.mapper.UserRoleMapper;
import com.lam.dormitory.service.IUserService;
import com.lam.dormitory.util.ResponseTemplate;
import com.lam.dormitory.vo.UserDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lam
 * @since 2019-12-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public User selectUserByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getUsername, username);
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public List<UserDetailVo> selectByUser(User user) {
        List<UserDetailVo> userList = this.baseMapper.selectByUser(user);
        return userList;
    }

    @Override
    public ResponseTemplate addAssignRole(Long userId, List<Long> roleIds) {
        try {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            QueryWrapper<UserRole> wrapper = new QueryWrapper<>(userRole);
            userRoleMapper.delete(wrapper);
            for (Long roleId : roleIds) {
                userRole.setRoleId(roleId);
                userRoleMapper.insert(userRole);
                if (roleId == 3) {//添加学生
                    QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
                    studentQueryWrapper.lambda().eq(Student::getUserId, userId);
                    Student selectOne = studentMapper.selectOne(studentQueryWrapper);
                    if (selectOne == null) {
                        Student student = new Student();
                        student.setUserId(userId);
                        student.setStatus(1);
                        studentMapper.insert(student);
                    }
                } else if (roleId == 2) {//添加管理员
                    QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
                    adminQueryWrapper.lambda().eq(Admin::getUserId, userId);
                    Admin selectOne = adminMapper.selectOne(adminQueryWrapper);
                    if (selectOne == null) {
                        Admin admin = new Admin();
                        admin.setUserId(userId);
                        admin.setStatus(1);
                        adminMapper.insert(admin);
                    }
                }
            }
            return new ResponseTemplate(200, "分配角色成功");
        } catch (Exception e) {
            return new ResponseTemplate(500, "分配角色成功");
        }
    }

    @Override
    public List<User> selectByRoleId(Long roleId) {
        return this.baseMapper.selectByRoleId(roleId);
    }


}
